package com.dingpeikeji.quickdeer.modules.ordersettle.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.service.OfficeService;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.ordersettle.entity.TOrdersSettle;
import com.dingpeikeji.quickdeer.modules.ordersettle.service.TOrdersSettleService;

/**
 * 客运公司申请结算表Controller
 * 
 * @author mash
 * @version 2017-03-13
 */
@Controller
@RequestMapping(value = "${adminPath}/ordersettle/tOrdersSettle")
public class TOrdersSettleController extends BaseController {

	@Autowired
	private TOrdersSettleService tOrdersSettleService;
	
	@Autowired
	private TOrdersService tOrdersService;
	
	@Autowired
	private OfficeService officeService;

	@ModelAttribute
	public TOrdersSettle get(@RequestParam(required = false) String id) {
		TOrdersSettle entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = tOrdersSettleService.get(id);
		}
		if (entity == null) {
			entity = new TOrdersSettle();
		}
		return entity;
	}

	@RequiresPermissions("ordersettle:tOrdersSettle:view")
	@RequestMapping(value = { "list", "" })
	public String list(TOrdersSettle tOrdersSettle, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		if (StringUtils.isEmpty(tOrdersSettle.getCreateDateQueryStart())
				&& StringUtils.isEmpty(tOrdersSettle.getCreateDateQueryEnd())) {
			tOrdersSettle.setCreateDateQueryStart(DateUtils.getDate("yyyy-MM-dd"));
			tOrdersSettle.setCreateDateQueryEnd(DateUtils.getDate("yyyy-MM-dd"));
		}
		Page<TOrdersSettle> page = tOrdersSettleService.findPage(new Page<TOrdersSettle>(request, response),
				tOrdersSettle);
		model.addAttribute("page", page);
		model.addAttribute("tOrdersSettle", tOrdersSettle);
		return "modules/ordersettle/tOrdersSettleList";
	}

	@RequiresPermissions("ordersettle:tOrdersSettle:view")
	@RequestMapping(value = { "buslist" })
	public String buslist(TOrdersSettle tOrdersSettle, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		// 查询条件
		if (StringUtils.isEmpty(tOrdersSettle.getCreateDateQueryStart())
				&& StringUtils.isEmpty(tOrdersSettle.getCreateDateQueryEnd())) {
			tOrdersSettle.setCreateDateQueryStart(DateUtils.getDate("yyyy-MM-dd"));
			tOrdersSettle.setCreateDateQueryEnd(DateUtils.getDate("yyyy-MM-dd"));
		}
		// 只能查本公司
		User user = UserUtils.getUser();
		if (!user.isAdmin())
			tOrdersSettle.setOfficeid(user.getOffice());

		Page<TOrdersSettle> page = tOrdersSettleService.findPage(new Page<TOrdersSettle>(request, response),
				tOrdersSettle);
		model.addAttribute("page", page);
		model.addAttribute("tOrdersSettle", tOrdersSettle);
		return "modules/ordersettle/tOrdersSettleBusList";
	}

	@RequiresPermissions("ordersettle:tOrdersSettle:view")
	@RequestMapping(value = "form")
	public String form(TOrdersSettle tOrdersSettle, Model model) {
		List<TOrders> page = tOrdersService.findOrderListBySettleId(tOrdersSettle.getSettleid());
		
		model.addAttribute("orderlist", page);
		model.addAttribute("tOrdersSettle", tOrdersSettle);
		return "modules/ordersettle/tOrdersSettleForm";
	}
	
	@RequiresPermissions("ordersettle:tOrdersSettle:view")
	@RequestMapping(value = "busform")
	public String busform(TOrdersSettle tOrdersSettle, Model model) {
		List<TOrders> page = tOrdersService.findOrderListBySettleId(tOrdersSettle.getSettleid());
		
		model.addAttribute("orderlist", page);
		model.addAttribute("tOrdersSettle", tOrdersSettle);
		return "modules/ordersettle/tOrdersSettleBusForm";
	}

	@RequiresPermissions("ordersettle:tOrdersSettle:edit")
	@RequestMapping(value = "confirmform")
	public String confirmform(TOrdersSettle tOrdersSettle, Model model) {
		if (tOrdersSettle.getRcptmoney() == null)
			tOrdersSettle.setRcptmoney(tOrdersSettle.getSettlemoney());
		if (tOrdersSettle.getDeductmoney() == null)
			tOrdersSettle.setDeductmoney("0.00");
		
		model.addAttribute("tOrdersSettle", tOrdersSettle);
		return "modules/ordersettle/tConfirmSettleForm";
	}

	@RequiresPermissions("ordersettle:tOrdersSettle:edit")
	@RequestMapping(value = "checkform")
	public String checkform(TOrdersSettle tOrdersSettle, Model model) {
		model.addAttribute("tOrdersSettle", tOrdersSettle);
		return "modules/ordersettle/tCheckSettleForm";
	}

	@RequiresPermissions("ordersettle:tOrdersSettle:rcpt")
	@RequestMapping(value = "rcptform")
	public String rcptform(TOrdersSettle tOrdersSettle, Model model) {
		model.addAttribute("tOrdersSettle", tOrdersSettle);
		return "modules/ordersettle/tRcptSettleForm";
	}

	@RequiresPermissions("ordersettle:tOrdersSettle:edit")
	@RequestMapping(value = "applyform")
	public String applyform(TOrdersSettle tOrdersSettle, HttpServletRequest request, HttpServletResponse response, Model model) {
		TOrders tOrders = new TOrders();
		//tOrders.setOrderstatus(TOrders.STATUS_PAY);
		User user = UserUtils.getUser();	
		//只能获取前一天完单的订单
		tOrders.setOffice(user.getOffice());
		List<TOrders> page = tOrdersService.findSettleList(tOrders);	
		model.addAttribute("orderlist", page);
		
	
		//默认结算单的一些值
		//计算可结算金额
		if (tOrdersSettle.getSettlemoney() == null) {
			double tmpMoney = 0.0;
			for (int i = 0; i < page.size(); i ++) {
				tmpMoney +=  page.get(i).getSettlemoney();
			}
			tOrdersSettle.setSettlemoney(String.valueOf(tmpMoney));
		}
		
		
		Office tmp = new Office();
		tmp = officeService.get(user.getOffice().getId());
		
		if (tOrdersSettle.getTitle() == null)
			tOrdersSettle.setTitle("常州市快鹿假期旅行社有限公司");
		if (tOrdersSettle.getRcptcontent() == null)
			tOrdersSettle.setRcptcontent("代办租车费");
		if (tOrdersSettle.getAcctname() == null)
			tOrdersSettle.setAcctname(tmp.getName());
		if (tOrdersSettle.getBankname() == null)
			tOrdersSettle.setBankname(tmp.getBankname());		
		if (tOrdersSettle.getBankaccount() == null)
			tOrdersSettle.setBankaccount(tmp.getBankaccount());
		
		model.addAttribute("tOrdersSettle", tOrdersSettle);
		return "modules/ordersettle/tApplySettleForm";
	}

	@RequiresPermissions("ordersettle:tOrdersSettle:edit")
	@RequestMapping(value = "save")
	public String save(TOrdersSettle tOrdersSettle, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tOrdersSettle)) {
			return form(tOrdersSettle, model);
		}

		tOrdersSettleService.save(tOrdersSettle);
		addMessage(redirectAttributes, "保存客运公司申请结算成功");
		return "redirect:" + Global.getAdminPath() + "/ordersettle/tOrdersSettle/buslist?repage";
	}

	@RequiresPermissions("ordersettle:tOrdersSettle:edit")
	@RequestMapping(value = "confirmsave")
	public String confirmsave(TOrdersSettle tOrdersSettle, Model model, RedirectAttributes redirectAttributes) {
		tOrdersSettle.setSettlestatus(TOrdersSettle.STATUS_PERMITTED);
		tOrdersSettleService.confirmsave(tOrdersSettle);
		addMessage(redirectAttributes, "确认开票成功");
		return "redirect:" + Global.getAdminPath() + "/ordersettle/tOrdersSettle/?repage";
	}

	@RequiresPermissions("ordersettle:tOrdersSettle:edit")
	@RequestMapping(value = "checksave")
	public String checksave(TOrdersSettle tOrdersSettle, Model model, RedirectAttributes redirectAttributes) {
		tOrdersSettle.setSettlestatus(TOrdersSettle.STATUS_CHECKED);
		tOrdersSettleService.checksave(tOrdersSettle);
		addMessage(redirectAttributes, "确认打款成功");
		return "redirect:" + Global.getAdminPath() + "/ordersettle/tOrdersSettle/?repage";
	}

	@RequiresPermissions("ordersettle:tOrdersSettle:rcpt")
	@RequestMapping(value = "rcptsave")
	public String rcptsave(TOrdersSettle tOrdersSettle, Model model, RedirectAttributes redirectAttributes) {
		tOrdersSettle.setSettlestatus(TOrdersSettle.STATUS_WAITCHECK);
		tOrdersSettleService.rcptsave(tOrdersSettle);
		addMessage(redirectAttributes, "开票成功");
		return "redirect:" + Global.getAdminPath() + "/ordersettle/tOrdersSettle/buslist?repage";
	}

	@RequiresPermissions("ordersettle:tOrdersSettle:edit")
	@RequestMapping(value = "delete")
	public String delete(TOrdersSettle tOrdersSettle, RedirectAttributes redirectAttributes) {
		tOrdersSettleService.delete(tOrdersSettle);
		addMessage(redirectAttributes, "删除客运公司申请结算成功");
		return "redirect:" + Global.getAdminPath() + "/ordersettle/tOrdersSettle/?repage";
	}

}